﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;

namespace MediaRecommender.Model
{
    //Methods to serialize/deserialize and write/read from file
    public class ObjectToFile
    {

        private BinaryFormatter bFormatter = new BinaryFormatter();

        public ObjectToFile()
        {
        }


        public void SerializeObject<T>(string filename, T objectToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create); //opens a filestream
            bFormatter.Serialize(stream, objectToSerialize); //the binaryformatter serializes the data and writes to the filestream
            stream.Close();
        }


        public T DeSerializeObject<T>(string filename)
        {
            T objectToDeserialize;
            Stream stream = File.Open(filename, FileMode.Open);
            objectToDeserialize = (T)bFormatter.Deserialize(stream);
            stream.Close();
            return objectToDeserialize;
        }
    }
}